package com.yhh.studysys.test.thread;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author howe
 * @Desc
 * @Date: 2021/3/16 14:29
 */
@Slf4j
public class ExportExcelThread<T> implements Callable<ExcelWriter> {

    private final int index;

    private final ExcelWriter excelWriter;

    private final List<?> data;

    public ExportExcelThread(int index, ExcelWriter writer, List<?> data){
        this.index = index;
        this.excelWriter = writer;
        this.data = data;
    }

    @Override
    public ExcelWriter call() {
        WriteSheet writeSheet = EasyExcel.writerSheet(index, "sheet" + index)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .build();
        excelWriter.write(data,writeSheet);
        log.info("线程:{},完成sheet{}导出" ,Thread.currentThread().getName(),index);
        return excelWriter;
    }
}
